package com.blrs.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.blrs.common.annotation.Anonymous;
import com.blrs.system.domain.SysUploadFile;
import com.blrs.system.service.ISysUploadFileService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;

/**
 * 附件Controller
 * 
 * @author yuxiang.chen
 * @date 2023-07-20
 */
@RestController
@RequestMapping("/system/file")
public class SysUploadFileController extends BaseController
{
    @Autowired
    private ISysUploadFileService sysUploadFileService;

    /**
     * 查询附件列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysUploadFile sysUploadFile)
    {
        startPage();
        List<SysUploadFile> list = sysUploadFileService.selectSysUploadFileList(sysUploadFile);
        return getDataTable(list);
    }
    @GetMapping("/queryFile/{businessId}/{businessCode}")
    public AjaxResult queryFile(@PathVariable("businessId")String businessId,@PathVariable("businessCode")String businessCode)
    {
        SysUploadFile sysUploadFile=new SysUploadFile();
        sysUploadFile.setBusinessId(businessId);
        sysUploadFile.setBusinessCode(businessCode);
        List<SysUploadFile> list = sysUploadFileService.selectSysUploadFileList(sysUploadFile);
        return success(list);
    }
    /**
     * 导出附件列表
     */
    //@PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUploadFile sysUploadFile)
    {
        List<SysUploadFile> list = sysUploadFileService.selectSysUploadFileList(sysUploadFile);
        ExcelUtil<SysUploadFile> util = new ExcelUtil<SysUploadFile>(SysUploadFile.class);
        util.exportExcel(response, list, "附件数据");
    }

    /**
     * 获取附件详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sysUploadFileService.selectSysUploadFileById(id));
    }

    /**
     * 新增附件
     */
    //@PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUploadFile sysUploadFile)
    {
        return toAjax(sysUploadFileService.insertSysUploadFile(sysUploadFile));
    }

    /**
     * 修改附件
     */
    //@PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUploadFile sysUploadFile)
    {
        return toAjax(sysUploadFileService.updateSysUploadFile(sysUploadFile));
    }

    /**
     * 删除附件
     */
   // @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sysUploadFileService.deleteSysUploadFileByIds(ids));
    }
    /**
     * 删除附件
     */
    //@PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "附件删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}/{type}")
    public AjaxResult fileRremove(@PathVariable("id")  String id,@PathVariable("type") String type)
    {
        return toAjax(sysUploadFileService.fileRremove(id,type));
    }
}
